package dao;

import com.alibaba.fastjson.JSON;
import pojo.order.BidEnitity;
import pojo.order.OrderEntity;
import utils.DataFileName;
import utils.DataUtils;
import utils.JsonUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Limohan
 */
public class OrderDao {
    public void saveOrder(OrderEntity order) {
        String json = JSON.toJSONString(order);
        try {
            DataUtils.writeData(DataFileName.ORDER.toString(), json);
        } catch (NullPointerException | IOException e) {
            e.printStackTrace();
        }
    }

    public List<OrderEntity> findOrderList() throws IOException {
        String json = DataUtils.readData(DataFileName.ORDER.getName());
        if (json.length() == 0) {
            return null;
        }
        List<OrderEntity> orderEntities = new ArrayList<>();
        String[] types = json.split("/");
        for (String str : types) {
            OrderEntity orderEntity = JsonUtil.jsonToObject(str, OrderEntity.class);
            orderEntities.add(orderEntity);
        }
        return orderEntities;
    }

    public OrderEntity findOrder(String id) throws IOException {
        List<OrderEntity> orderEntityList = findOrderList();
        if (orderEntityList != null) {
            for (OrderEntity orderEntity : orderEntityList) {
                if (id.equals(orderEntity.getId())) {
                    return orderEntity;
                }
            }
        }
        return null;
    }
    public List<OrderEntity> findOrderByConsignee(String consignee) throws IOException {
        List<OrderEntity> orderEntities = findOrderList();
        List<OrderEntity> orderEntities1 = new ArrayList<>();
        if (orderEntities != null) {
            for (OrderEntity orderEntity : orderEntities) {
                if (consignee.equals(orderEntity.getConsignee())) {
                    orderEntities1.add(orderEntity);
                }
            }
            return orderEntities1;
        }
        return null;
    }

    public String getMaxId() throws IOException {
        List<OrderEntity> orderEntities = findOrderList();
        int max = 0;
        if (orderEntities == null || orderEntities.size() == 0) {
            return "1";
        } else {
            for (OrderEntity orderEntity : orderEntities) {
                if (Integer.parseInt(orderEntity.getId()) > max) {
                    max = Integer.parseInt(orderEntity.getId());
                }
            }
            return String.valueOf(max + 1);
        }
    }

    public void changeCustomerState(List<OrderEntity> orders) throws IOException {
        List<OrderEntity> orderEntities = findOrderList();
        if (orderEntities == null || orders == null) return;
        for (OrderEntity orderEntity : orderEntities) {
            for(OrderEntity orderEntity1:orders){
                if (orderEntity.getId().equals(orderEntity1.getId())){
                    orderEntity.setOrderStateForCustomer("已发布");
                    orderEntity.setOrderStateForFactory("可投标");
                }
            }

        }
        DataUtils.clearData(DataFileName.ORDER.toString());
        for (OrderEntity order : orderEntities) {
            this.saveOrder(order);
        }
    }

    public void deleteOrder(String id) throws IOException {
        List<OrderEntity> orderEntities = findOrderList();
        orderEntities.removeIf(orderEntity -> orderEntity.getId().equals(id));
        DataUtils.clearData(DataFileName.ORDER.toString());
        for (OrderEntity orderEntity : orderEntities) {
            this.saveOrder(orderEntity);
        }
    }

    public List<OrderEntity> findFactoryOrder() throws IOException {
        List<OrderEntity> orderEntities = findOrderList();
        List<OrderEntity> orderEntities1 = new ArrayList<>();
        if(orderEntities!=null){
            for(OrderEntity orderEntity:orderEntities){
                if(orderEntity.getOrderStateForCustomer().equals("已发布")||orderEntity.getOrderStateForCustomer().equals("已收货")||orderEntity.getOrderStateForCustomer().equals("已发货"))
                    orderEntities1.add(orderEntity);
            }
               return orderEntities1;
        }
        return null;
    }
    public void setBidPrice(String price,String id,String factory) throws IOException {
        List<OrderEntity> orderEntities = findOrderList();
        for(OrderEntity orderEntity:orderEntities){
            if(orderEntity.getId().equals(id)){
                BidEnitity bidEnitity = new BidEnitity();
                bidEnitity.setFactory(factory);
                bidEnitity.setIsChosen("未中标");
                bidEnitity.setName(orderEntity.getName());
                bidEnitity.setPhone(orderEntity.getPhone());
                bidEnitity.setPrice(price);
                bidEnitity.setId("1");
                String bidId =this.getMaxId();
                bidEnitity.setId(bidId);
                orderEntity.setOrderStateForFactory("已投标");
                List<BidEnitity> bids = orderEntity.getBids();
                if(bids==null) bids=new ArrayList<>();
                bids.add(bidEnitity);
                orderEntity.setBids(bids);
                }
            }
        DataUtils.clearData(DataFileName.ORDER.toString());
        for (OrderEntity order : orderEntities) {
            this.saveOrder(order);
        }
    }

  // //一个经销商所有被投标的订单
  // public List<OrderEntity> findGetBidOrderForCustomer(String customer) throws IOException {
  //     List<OrderEntity> orderEntities = findOrderList();
  //     List<OrderEntity> orderEntities1 = new ArrayList<>();
  //     if(orderEntities!=null){
  //         for(OrderEntity orderEntity:orderEntities){
  //             if(orderEntity.getOrderStateForFactory().equals("已投标")&&orderEntity.getConsignee().equals(customer))
  //                 orderEntities1.add(orderEntity);
  //         }
  //         return orderEntities1;
  //     }
  //     return null;
  // }

    //客户选标
    public void chooseBid(BidEnitity bidEnitity,String OrderId) throws IOException {
        List<OrderEntity> orderEntities = findOrderList();
        for(OrderEntity orderEntity:orderEntities){
            if(orderEntity.getId().equals(OrderId)) {
                for (BidEnitity bidEnitity1:orderEntity.getBids()){
                    if(bidEnitity.getFactory().equals(bidEnitity1.getFactory()))
                        bidEnitity1.setIsChosen("已中标");
                        orderEntity.setOrderStateForFactory("投标结束");
                        orderEntity.setFactory(bidEnitity1.getFactory());
                }
            }
        }
        DataUtils.clearData(DataFileName.ORDER.toString());
        for (OrderEntity order : orderEntities) {
            this.saveOrder(order);
        }
    }

    public BidEnitity findBidByFactory(String orderId,String factory) throws IOException {
        List<OrderEntity> orderEntities = findOrderList();
        for(OrderEntity orderEntity:orderEntities){
            if(orderEntity.getId().equals(orderId)) {
                for (BidEnitity bidEnitity1:orderEntity.getBids()){
                    if(bidEnitity1.getFactory().equals(factory))
                       return bidEnitity1;
                }
            }
        }
        return null;
    }

    public void finishPaiChan(String orderId) throws IOException {
        List<OrderEntity> orderEntities = findOrderList();
        if(orderEntities!=null){
            for(OrderEntity orderEntity:orderEntities){
                if(orderEntity.getId().equals(orderId)){
                    orderEntity.setOrderStateForFactory("已排产");
                }
            }
            DataUtils.clearData(DataFileName.ORDER.toString());
            for (OrderEntity order : orderEntities) {
                this.saveOrder(order);
            }
        }
    }

    public void finishWork(String orderId) throws IOException {
        List<OrderEntity> orderEntities = findOrderList();
        if(orderEntities!=null){
            for(OrderEntity orderEntity:orderEntities){
                if(orderEntity.getId().equals(orderId)){
                    orderEntity.setOrderStateForFactory("已完工");
                }
            }
            DataUtils.clearData(DataFileName.ORDER.toString());
            for (OrderEntity order : orderEntities) {
                this.saveOrder(order);
            }
        }
    }
    public void finishOrder(String orderId) throws IOException {
        List<OrderEntity> orderEntities = findOrderList();
        if(orderEntities!=null){
            for(OrderEntity orderEntity:orderEntities){
                if(orderEntity.getId().equals(orderId)){
                    orderEntity.setOrderStateForFactory("已完成");
                }
            }
            DataUtils.clearData(DataFileName.ORDER.toString());
            for (OrderEntity order : orderEntities) {
                this.saveOrder(order);
            }
        }
    }

    public void faHuo(String orderId) throws IOException {
        List<OrderEntity> orderEntities = findOrderList();
        if(orderEntities!=null){
            for(OrderEntity orderEntity:orderEntities){
                if(orderEntity.getId().equals(orderId)){
                    orderEntity.setOrderStateForFactory("已发货");
                    orderEntity.setOrderStateForCustomer("已发货");
                }
            }
            DataUtils.clearData(DataFileName.ORDER.toString());
            for (OrderEntity order : orderEntities) {
                this.saveOrder(order);
            }
        }
    }

    public void shouHuo(String orderId) throws IOException {
        List<OrderEntity> orderEntities = findOrderList();
        if(orderEntities!=null){
            for(OrderEntity orderEntity:orderEntities){
                if(orderEntity.getId().equals(orderId)){
                    orderEntity.setOrderStateForFactory("已签收");
                    orderEntity.setOrderStateForCustomer("已收货");
                }
            }
            DataUtils.clearData(DataFileName.ORDER.toString());
            for (OrderEntity order : orderEntities) {
                this.saveOrder(order);
            }
        }
    }


}
